package demo;

public class ThreadSafe {
    private static int a;
    private static final int N=10_0000;

    static class SbuThread1 extends Thread{

        @Override
        public void run() {

            for(int i=0;i<N;i++){
                a++;
            }
        }
    }
    static class SubThread2 extends Thread{

        @Override
        public void run() {
            for(int i=0;i<N;i++){
                a--;
            }
        }
    }

    public static void main(String[] args)  throws Exception {
        SbuThread1 t1=new SbuThread1();
        SubThread2 t2=new SubThread2();

        t1.start();
        t2.start();
        //调用join方法，等待t1和t2线程结束，期待a==0,但结果不是
        //没得到到预想的结果，线程不安全
        t1.join();
        t2.join();
        System.out.println(a);
    }
}
